package thinkingInJava.concurrency;
// page 662 后台线程(daemon,主线程退出时它会自动kill)
//: concurrency/SimpleDaemons.java
//Daemon threads don't prevent the program from ending.
import java.util.concurrent.*;
import static thinkingInJava.YTool.YPrint.*;

public class SimpleDaemons implements Runnable {
	public void run() {
		try {
			while(true) {
				TimeUnit.MILLISECONDS.sleep(100);
				print(Thread.currentThread() + " " + this);
			}
		} catch(InterruptedException e) {
			print("sleep() interrupted");
		}
	}
	public static void main(String[] args) throws Exception {
		for(int i = 0; i < 10; i++) {
			Thread daemon = new Thread(new SimpleDaemons());
			// 设置成为后台线程,如果不设置则此线程不会因为 main 执行完而退出,设置了就会.
			daemon.setDaemon(true); // Must call before start()
			daemon.start();
		}
		print("All daemons started");
		TimeUnit.MILLISECONDS.sleep(175);
	}
} /* Output: (Sample)
All daemons started
Thread[Thread-0,5,main] SimpleDaemons@530daa
Thread[Thread-1,5,main] SimpleDaemons@a62fc3
Thread[Thread-2,5,main] SimpleDaemons@89ae9e
Thread[Thread-3,5,main] SimpleDaemons@1270b73
Thread[Thread-4,5,main] SimpleDaemons@60aeb0
Thread[Thread-5,5,main] SimpleDaemons@16caf43
Thread[Thread-6,5,main] SimpleDaemons@66848c
Thread[Thread-7,5,main] SimpleDaemons@8813f2
Thread[Thread-8,5,main] SimpleDaemons@1d58aae
Thread[Thread-9,5,main] SimpleDaemons@83cc67
...
 *///:~
